from flask import Blueprint,request
from model import model
from model.model import *
from db_init import db
import re
from datetime import datetime,date
from tools.catch import redis_cli

userlist = Blueprint('userlist',__name__,url_prefix='/userlist')

# 获取用户信息
@userlist.route('/get_user',methods=['get'])
def get_user():
    uid = request.args.get('u_id')
    user_data = User.query.filter_by(id=uid).first()
    user_list = [{
        'name': user_data.name,
        'password': user_data.password,
        'img': user_data.img,
        'mobile': user_data.mobile,
        'money': user_data.money
    }]
    return {'msg':'ok','code':200,'data':user_list}

# 更新用户信息
@userlist.route('/update_user',methods=['put'])
def update_user():
    uid = request.form.get('id')
    name_fount = request.form.get('name')
    password_fount = request.form.get('password')
    mobile_fount = request.form.get('mobile')
    img_fount = request.form.get('img')
    money_fount = request.form.get('money')
    if not all([name_fount,password_fount,mobile_fount,money_fount]):
        return {'msg':'前端数不完整','code':400}
    User.query.filter_by(id=uid).update({
        'name':name_fount,
        'password':password_fount,
        'img':img_fount,
        'mobile':mobile_fount,
        'money':money_fount
    })
    db.session.commit()
    return {'msg':'修改成功','code':200}

# 增加角色
@userlist.route('/add_role',methods=['post'])
def add_role():
    name = request.form.get('name')
    user_id = request.form.get('user_id')
    if not all([name,user_id]):
        return {'msg':'前端数据不完整','code':400}
    role = Role(name=name,user_id=user_id)
    db.session.add(role)
    db.session.commit()
    return '添加成功'

# 获取所有的角色
@userlist.route('/get_all_role',methods=['get'])
def get_all_role():
    role_data = Role.query.filter().all()
    role_list = [{'id':i.id,'name':i.name}for i in role_data]
    return {'msg':'ok','code':200,'data':role_list}

# 获取所有的权限
@userlist.route('/get_all_power',methods=['get'])
def get_all_power():
    power = Power.query.filter().all()
    power_list = [{'id':i.id,'name':i.name,'url':i.url,'method':i.method}for i in power]
    return {'msg':'ok','code':200,'data':power_list}

# 添加角色
@userlist.route('/role',methods=['post'])
def add_role():
    name = request.json.get('name')
    power = request.json.get('power')
    if not name:
        return {'msg':'前端传的数据不完整','code':400}
    role = Role(name=name)
    role.power.extend(Power.query.filter(Power.id.in_(power)).all())
    db.session.add(role)
    db.session.commit()
    return {'msg':'角色添加成功','code':200}
# @userlist.route('/user_power',methods=['put','post'])
# def user_power():
#     user_id = request.form.get('user_id')
#     role_name = request.form.get('role_name')
#     per_id = request.form.get('per_id')
#     if not all([user_id,role_name,per_id]):
#         return {'msg':'前端数据不完整','code':400}
#     user_obj = []
#     for i in user_id:
#         user = User.query.filter_by(id=i).first()
#         if user:
#             user_obj.append(user)
#         else:
#             return {'msg':'没有这个用户','code':400}
#     power_obj = []
#     for i in per_id:
#         per = Power.query.filter_by(id=i).first()
#         if per:
#             power_obj.append(per)
#         else:
#             return {'msg':'没有这个权限','code':400}
#     try:
#         role = User(role_id=role_name,user_id=user_id)
#         db.session.add(role)
#         db.session.commit()
#         try:
#             user_data = User(*user_obj)
#             db.session.add(user_data)
#             db.session.commit()
#         except Exception as e:
#             print(e)
#             return {'msg':'添加用户角色失败','code':400}
#         try:
#             for i in power_obj:
#                 power = Power(name=role,user_id=i.user_id,power_id=i.power_id)
#                 db.session.add(power)
#                 db.session.commit()
#                 return {'msg':'添加成功','code':200}
#         except Exception as e:
#             print(e)
#             return {'msg':'角色添加权限失败','code':400}
#     except Exception as e:
#         print(e)
#         return {'msg':'添加角色失败','code':400}

#增加种类

@userlist.route('/add_cate',methods=['post'])
def cate_add():
    c_name = request.form.get('cate')
    cate = Cate(c_name=c_name)
    db.session.add(cate)
    db.session.commit()
    return '种类添加成功'

# 修改种类
@userlist.route('/cate_update',methods=['put'])
def cate_update():
    id = request.form.get('id')
    c_name = request.form.get('c_name')
    if not all([id,c_name]):
        return {'msg':'前端数据不完整','code':400}
    Cate.query.filter_by(id=id).update(c_name=c_name)
    db.session.commit()
    return {'msg':'种类修改成功','code':200}

# 删除种类
@userlist.route('/cate_delete',methods=['delete'])
def cate_delete():
    id = request.form.get('id')
    Cate.query.filter_by(id=id).delete()
    db.session.commit()
    return {'msg':'删除成功','code':200}

# 给商家提供信息商家新的商品
@userlist.route('/goods_add',methods=['post'])
def goods_add():
    g_name = request.form.get('g_name')
    count_num = request.form.get('count_num')
    countent = request.form.get('countent')
    price = request.form.get('price')
    cate_id = request.form.get('cate_id')
    if not all([g_name,count_num,countent,cate_id,price]):
        return {'msg':'前端数据不玩获整','code':400}
    goods = Goods.queyr.filter_by(g_name=g_name).first()
    if goods:
        return {'msg':'该商品已经存在，请不要重复添加','code':400}
    goods_data = Goods(g_name=g_name,count_num=count_num,countent=countent,cate_id=cate_id,price=price)
    print(goods_data)
    db.session.add(goods_data)
    db.session.commit()
    return '添加成功'

# 获取商品信息
@userlist.route('/get_goods_info',methods=['get'])
def get_goods_info():
    g_id = request.form.get('goods_id')
    goods = Goods.query.filter_by(id=g_id).first()
    if not goods:
        return {'msg':'没有这个商品','code':400}
    goods_list = [{'id':i.id,'g_name':i.g_name,'count_num':i.count_num,
                  'price':i.price,'cate_id':i.cate_id }for i in goods]
    return {'msg':'ok','code':200,'data':goods_list}

# 修改商品信息
@userlist.route('/update_good',methods=['put'])
def update_good():
    g_id = request.form.get('gid')
    g_name = request.form.get('g_name')
    count_num = request.form.get('count_num')
    price = request.form.get('price')
    img = request.form.get('img')
    countent = request.form.get('countent')
    cate_id = request.form.get('cate_id')
    if not all([g_id,g_name,count_num,price,img,countent,cate_id]):
        return {'msg':'前端数据不完整','code':400}
    Goods.query.filter_by(id=g_id).update({
        'g_name':g_name,
        'count_num':count_num,
        'price':price,
        'img':img,
        'countent':countent,
        'cate_id':cate_id
    })
    db.session.commit()
    return {'msg':'更新成功','code':200}

#删除商品
@userlist.route('/delete_goods',methods=['delete'])
def goods_delete():
    gid = request.form.get('gid')
    Goods.query.filter_by(id=gid).delete()
    db.session.commit()
    return {'msg':'删除成功','code':200}

#添加商户信息
@userlist.route('/add_goods_h',methods=['post'])
def goods_h_add():
    g_name = request.form.get('g_name')
    g_addr = request.form.get('g_addr')
    is_defalue = request.form.get('is_defalue')
    if not all([g_name,g_addr,is_defalue]):
        return {'msg':'商户添加成功','code':200}
    goods_h_data = Goods_H(g_name=g_name,g_addr=g_addr,is_defalue=is_defalue)
    db.session.add(goods_h_data)
    db.session.commit()
    return {'msg':'商户添加成功','code':200}

#修改商户号信息
# 方法一，从模型类里面获取id直接修改
@userlist.route('/goods_h_update',methods=['put'])
def goods_h_update():
    goods_h = request.json
    g_id = goods_h.pop('id')
    Goods_H.query.filter_by(id=g_id).update(goods_h)
    db.session.commit()
    return {'msg':'修改成功','code':200,'data':goods_h}

#修改商户号信息
# 方法二写表名，从路由里面直接修改
@userlist.route('/<table_name>',methods=['put'])
def goods_h_jjj(table_name):
    # 看表首字母是不是大写，驼峰
    table = table_name.title()
    # 前端传来的数据
    good = request.json
    # 给id重新赋值给新的变量
    g_id = good.pop('id')
    if hasattr(model,table):
        getattr(model,table).query.filter_by(id=g_id).update(good)
    else:
        return {'msg':'url错误','code':400}
    db.session.commit()
    return {'msg':'商品修改成功','code':200}

# 删除商户信息
@userlist.route('/goods_h_delete',methods=[])
def goods_h_delete():
    gid = request.form.get('id')
    Goods_H.query.fiter_by(id=gid).delete()
    db.session.commit()
    return {'msg':'商户删除成功','code':200}






